package com.jacen.fzxs.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jacen.fzxs.entity.WallEntity;
import com.jacen.fzxs.service.IBaseService;
import com.jacen.fzxs.service.IWallService;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.EntityUtils;
import com.jacen.fzxs.util.MD5;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.XmlUtil;


/**
 * 用户消息处理类
 * @author Jacen
 * 2014年8月20日 下午7:58:50
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WeiXinService {
	
	
	private String MsgType = "";
	private Element root;
	private HttpServletResponse response ;
	
	@Resource
	private IWallService wallService;
	
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Element getRoot() {
		return root;
	}

	public void setRoot(Element root) {
		this.root = root;
	}

	public String getMsgType() {
		return MsgType;
	}

	public void setMsgType(String msgType) {
		MsgType = msgType;
	}
	
	public void service(IBaseService<?> service){
		
		if(this.MsgType.equalsIgnoreCase("text")){
			parseText(service,root, response);
		}
		if(this.MsgType.equalsIgnoreCase("image")){
			parseImage(root, response);
		}
		if(this.MsgType.equalsIgnoreCase("voice")){
			//声音消息
			parseVoice(root, response);
		}
		if(this.MsgType.equalsIgnoreCase("location")){
			//位置信息
			parseLocation(service,root, response);
		}
		if(this.MsgType.equalsIgnoreCase("event")){
			//事件
			String event = root.element("Event").getText();
			String toUserName = root.element("ToUserName").getText();
			String fromUserName = root.element("FromUserName").getText();
			if("subscribe".equalsIgnoreCase(event)){
				//关注事件
				answerText(fromUserName, toUserName, "欢迎关注", response);
			}
			if("unsubscribe".equalsIgnoreCase(event)){
				//取消关注
			}
		}
	}

	/**
	 * 解析声音
	 * @param root
	 * @param response
	 */
	private void parseVoice(Element root, HttpServletResponse response) {
		String ToUserName = root.element("ToUserName").getText();
		String FromUserName = root.element("FromUserName").getText();
		String MediaId = root.element("MediaId").getText();
		answerVoice(FromUserName,ToUserName,MediaId,response);
	}

	/**
	 * 解析图片
	 * @param root
	 * @param response
	 */
	private void parseImage(Element root, HttpServletResponse response) {
		String ToUserName = root.element("ToUserName").getText();
		String FromUserName = root.element("FromUserName").getText();
		String MediaId = root.element("MediaId").getText();
		answerImage(FromUserName,ToUserName,MediaId,response); 
	}

	
	/**
	 * 解析文本
	 * @param root
	 * @param response
	 */
	private void parseText(IBaseService<?> service,Element root,HttpServletResponse response) {
		String ToUserName = root.element("ToUserName").getText();
		String FromUserName = root.element("FromUserName").getText();
		String content = root.element("Content").getText();//姓名 电话
		if(content==null || "".equals(content)){
			answerText(FromUserName,ToUserName,"请输入手机号",response);
		}
		System.out.println(content);
		Map<String, String> params = new HashMap<String,String>();
		params.put("tel", content);
		try{
			List<WallEntity> list = ((IWallService)service).getEntityList(params);
			if(list==null || list.size()==0){
				answerText(FromUserName,ToUserName,"未查到相关信息",response);
				return;
			}
			StringBuffer sb = new StringBuffer();
			sb.append("-------------------\n");
			for(WallEntity entity : list){
				sb.append(entity.getUsername()+";"+entity.getCity()+""+entity.getAddress()+"\n");
			}
			sb.append("-------------------\n");
			answerText(FromUserName,ToUserName,sb.toString(),response);
		}catch(Exception e){
			e.printStackTrace();
			answerText(FromUserName,ToUserName,"未查到相关信息",response);
		}
		 
	}
	
	/**
	 * 解析位置信息添加墙体
	 * @param root
	 * @param response
	 */
	private void parseLocation(IBaseService<?> service,Element root, HttpServletResponse response) {
		String toUserName = root.element("ToUserName").getText();
		String fromUserName = root.element("FromUserName").getText();
		String label = root.element("Label").getText();
		String locationX = root.element("Location_X").getText();
		String locationY = root.element("Location_Y").getText();
		WallEntity entity = new WallEntity();
		entity.setId(EntityUtils.createId32());
		entity.setAddress(label);
		entity.setLocationX(Double.valueOf(locationX));
		entity.setLocationY(Double.valueOf(locationY));
		entity.setCreateTime(DateUtils.getNowFormatTime(DateUtils.NORMAL_FORMAT));
		try {
			((IWallService)service).insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
			answerText(fromUserName,toUserName,"添加失败，服务器异常："+e.getMessage(),response); 
		}
		answerText(fromUserName,toUserName,"添加完成",response); 
	}

	private void answerText(String fromUserName,String toUserName,String context,HttpServletResponse response) {
		String rspXml = XmlUtil.getTextXml(toUserName, fromUserName, context);
		ServletUtils.outputHTML(response, rspXml);
	}
	
	private void answerImage(String fromUserName, String toUserName,
			String mediaId, HttpServletResponse response) {
		String rspXml = XmlUtil.getImageXml(toUserName, fromUserName, mediaId);
		ServletUtils.outputHTML(response, rspXml);
	}
	
	private void answerVoice(String fromUserName, String toUserName,
			String mediaId, HttpServletResponse response) {
		String rspXml = XmlUtil.getVoiceXml(toUserName, fromUserName, mediaId);
		ServletUtils.outputHTML(response, rspXml);
	}

	public static void main(String[] args) {
		System.out.println(MD5.MD5Encode("Jacen007"));
	}

}
